package mybox.engine.csv;

import java.awt.Polygon;

import mybox.engine.QVector;
import static java.lang.Math.*;

public class PolyFromBasic {
	
	double radius;
	Polygon polygon;
	public PolyFromBasic(
							QVector center, 
							QVector start, 
							QVector end, 
							double schrittWeite, 
							int intFactor, 
							int anticlockwise) 
			throws Exception {
		
		radius=QVector.diff(center, start).length();
		polygon=sectorFactory(center, 
						start, 
						end, 
					    schrittWeite, 
						intFactor, 
						anticlockwise);
	}
	
	public static Polygon sectorFactory(
							QVector center, 
							QVector start, 
							QVector end, 
							double schrittWeite, 
							int intFactor, 
							int anticlockwise) throws Exception {
		Polygon polygon=new Polygon();
		
		double radius=QVector.diff(center, start).length();
		double compareRadius=QVector.diff(center, end).length();
		if (abs(radius-compareRadius)>schrittWeite/100) 
			throw new Exception("start, end verschieden entfernt von center");
				
		polygon.addPoint((int)round(start.x*intFactor), (int)round(start.y*intFactor));
		double psiHalbe=anticlockwise*atan(schrittWeite/radius);
		double psi=psiHalbe*2;		
		QVector center2start=QVector.diff(start, center);
		double currPhi=atan2(center2start.y,center2start.x);
		double nextPhi=currPhi+psi;
		QVector nextPoint=new QVector(center.x+radius*cos(nextPhi),center.y+radius*sin(nextPhi));
		while (QVector.diff(nextPoint, end).length()>schrittWeite) {
			polygon.addPoint((int)round(nextPoint.x*intFactor), (int)round(nextPoint.y*intFactor));			
			nextPhi=nextPhi+psi;
			nextPoint=new QVector(center.x+radius*cos(nextPhi),center.y+radius*sin(nextPhi));
		}
		polygon.addPoint((int)round(end.x*intFactor), (int)round(end.y*intFactor));
		
		
		return polygon;
		
	}
	
	public static Polygon lineFactory( 
							QVector start, 
							QVector end, 							 
							int intFactor
							) {
		Polygon polygon=new Polygon();
		
		polygon.addPoint((int)round(start.x*intFactor), (int)round(start.y*intFactor));
		polygon.addPoint((int)round(end.x*intFactor), (int)round(end.y*intFactor));						
		return polygon;
		
	}
	

}
